package com.SinaMicrobloggingApi.Core.Process;

import java.util.ArrayList;
import java.util.List;

import com.SinaMicrobloggingApi.Core.Request.AccessTokenRequest;
import com.SinaMicrobloggingApi.Core.Request.AuthorizeRequest;
import com.SinaMicrobloggingApi.Core.Request.DirectMessagesRequest;
import com.SinaMicrobloggingApi.Core.Request.PublicTimeLineRequest;
import com.SinaMicrobloggingApi.Core.Request.RequestTokenRequest;
import com.SinaMicrobloggingApi.Core.Request.SendMessageRequest;
import com.SinaMicrobloggingApi.Core.Request.StatusesFollowersRequest;
import com.SinaMicrobloggingApi.Core.Request.StatusesUpdateRequest;
import com.SinaMicrobloggingApi.Core.Request.VerifyCredentialsRequest;
import com.SinaMicrobloggingApi.Core.Response.AccessTokenResponse;
import com.SinaMicrobloggingApi.Core.Response.AuthorizeResponse;
import com.SinaMicrobloggingApi.Core.Response.DirectMessagesResponse;
import com.SinaMicrobloggingApi.Core.Response.PublicTimeLineResponse;
import com.SinaMicrobloggingApi.Core.Response.RequestTokenResponse;
import com.SinaMicrobloggingApi.Core.Response.SendMessageResponse;
import com.SinaMicrobloggingApi.Core.Response.StatusesFollowersResponse;
import com.SinaMicrobloggingApi.Core.Response.StatusesUpdateResponse;
import com.SinaMicrobloggingApi.Core.Response.VerifyCredentialsResponse;
import com.SinaMicrobloggingApi.Model.DirectMessagesModel;
import com.SinaMicrobloggingApi.Model.StatusModel;
import com.SinaMicrobloggingApi.Model.UserInfoModel;

import com.SinaMicrobloggingApi.Core.Request.FriendTimeLineRequest;
import com.SinaMicrobloggingApi.Core.Response.FriendTimeLineResponse;

public class SinaMicrobloggingAPIProcess {
	public static UserInfoModel logging(String userName, String password) {
		// 1.first to get request token
		String token = "";
		String tokenSecret = "";
		RequestTokenResponse requestTokenresponse = new RequestTokenResponse();
		RequestTokenRequest requestTokenRequest = new RequestTokenRequest(
				requestTokenresponse);
		requestTokenresponse = (RequestTokenResponse) requestTokenRequest
				.getResponse();
		token = requestTokenresponse.getOauthToken();
		tokenSecret = requestTokenresponse.getOauthTokenSecret();
		// 2.to get Authorize
		AuthorizeResponse authorizeResponse = new AuthorizeResponse();
		AuthorizeRequest authorizeRequest = new AuthorizeRequest(
				authorizeResponse);
		authorizeRequest.setUserId(userName);
		authorizeRequest.setPassword(password);
		authorizeRequest.setOauthToken(token);
		authorizeRequest.setOauthTokenSecret(tokenSecret);
		authorizeResponse = (AuthorizeResponse) authorizeRequest.getResponse();
		// 3. to get AccessToken
		AccessTokenResponse accessTokenResponse = new AccessTokenResponse();
		AccessTokenRequest accessTokenRequest = new AccessTokenRequest(
				accessTokenResponse);
		accessTokenRequest.setOauthVerifier(authorizeResponse
				.getOauthVerifier());
		accessTokenRequest.setOauthToken(token);
		accessTokenRequest.setOauthTokenSecret(tokenSecret);

		accessTokenResponse = (AccessTokenResponse) accessTokenRequest
				.getResponse();
		token = accessTokenResponse.getOauthToken();
		tokenSecret = accessTokenResponse.getOauthTokenSecret();
		// confirm personal information
		VerifyCredentialsResponse verifyCredentialsResponse = new VerifyCredentialsResponse();
		VerifyCredentialsRequest verifyCredentialsRequest = new VerifyCredentialsRequest(
				verifyCredentialsResponse);
		verifyCredentialsRequest.setOauthToken(token);
		verifyCredentialsRequest.setOauthTokenSecret(tokenSecret);
		verifyCredentialsResponse = (VerifyCredentialsResponse) verifyCredentialsRequest
				.getResponse();
		UserInfoModel user = verifyCredentialsResponse.getUserInfo();
		user.setAccessToken(token);
		user.setAccessTokenSecret(tokenSecret);

		return user;
	}

	public static List<StatusModel> getPublicTimeLine(String accessToken,
			String accessTokenSecret, int count) {
		PublicTimeLineResponse publicTimeResponse = new PublicTimeLineResponse();
		PublicTimeLineRequest publicTimeRequest = new PublicTimeLineRequest(
				publicTimeResponse, count);
		publicTimeRequest.setOauthToken(accessToken);
		publicTimeRequest.setOauthTokenSecret(accessTokenSecret);
		publicTimeResponse = (PublicTimeLineResponse) publicTimeRequest
				.getResponse();

		return publicTimeResponse.getStatus();
	}
	
	/*
	 * --Time:2011.05.15
	 --------------------Get Friend_TimeLine------------------------------- 
	 * */
	public static List<StatusModel> getFriendTimeLine(String accessToken,
			String accessTokenSecret, int count) {
		FriendTimeLineResponse publicTimeResponse = new FriendTimeLineResponse();
		FriendTimeLineRequest publicTimeRequest = new FriendTimeLineRequest(
				publicTimeResponse, count);
		publicTimeRequest.setOauthToken(accessToken);
		publicTimeRequest.setOauthTokenSecret(accessTokenSecret);
		publicTimeResponse = (FriendTimeLineResponse) publicTimeRequest
				.getResponse();

		return publicTimeResponse.getStatus();
	}
	public static List<DirectMessagesModel> getDirectMessages(
			String accessToken, String accessTokenSecret, int count) {
		DirectMessagesResponse directMessagesResponse = new DirectMessagesResponse();
		DirectMessagesRequest directMessagesRequest = new DirectMessagesRequest(
				directMessagesResponse, count);
		directMessagesRequest.setOauthToken(accessToken);
		directMessagesRequest.setOauthTokenSecret(accessTokenSecret);
		directMessagesResponse = (DirectMessagesResponse) directMessagesRequest
				.getResponse();

		return directMessagesResponse.getDirectMessages();
	}

	public static StatusModel postANewStatus(String accessToken,
			String accessTokenSecret, String userId, String password,
			String content) {

		StatusesUpdateResponse statusesUpdateResponse = new StatusesUpdateResponse();
		StatusesUpdateRequest statusesUpdateRequest = new StatusesUpdateRequest(
				statusesUpdateResponse, userId, password, content);
		statusesUpdateRequest.setOauthToken(accessToken);
		statusesUpdateRequest.setOauthTokenSecret(accessTokenSecret);
		statusesUpdateResponse = (StatusesUpdateResponse) statusesUpdateRequest
				.getResponse();

		return statusesUpdateResponse.getNewStatus();
	}

	public static DirectMessagesModel sendMessage(String accessToken,
			String accessTokenSecret, int toUserId, String text) {

		SendMessageResponse sendMessageResponse = new SendMessageResponse();
		SendMessageRequest sendMessageRequest = new SendMessageRequest(
				sendMessageResponse, toUserId, text);
		sendMessageRequest.setOauthToken(accessToken);
		sendMessageRequest.setOauthTokenSecret(accessTokenSecret);
		sendMessageResponse = (SendMessageResponse) sendMessageRequest
				.getResponse();

		return sendMessageResponse.getSendMessage();
	}

	public static List<UserInfoModel> getUserFollowers(String accessToken,
			String accessTokenSecret) {
		StatusesFollowersResponse statusesFollowersResponse = new StatusesFollowersResponse();
		StatusesFollowersRequest statusesFollowersRequest = new StatusesFollowersRequest(
				statusesFollowersResponse);
		statusesFollowersRequest.setOauthToken(accessToken);
		statusesFollowersRequest.setOauthTokenSecret(accessTokenSecret);
		statusesFollowersResponse = (StatusesFollowersResponse) statusesFollowersRequest
				.getResponse();
		return statusesFollowersResponse.getFollwers();
	}
}
